import Fileio from '@ohos.fileio';

const baseFilePath = "/data/accounts/account_0/applications/ohos.samples.jshelloworld/ohos.samples.jshelloworld/assets/entry/resources/rawfile/";
const dataFile = "/data/accounts/account_0/appdata/ohos.samples.jshelloworld/cache/";
const fileName = "test_file.txt";
const READ_DATA_SIZE = 4096;

export default {

    readTestFile() {
        console.info("test usb FileUtil readJsonObj start execution");
        let filePath = dataFile + fileName;
        return this.readFile(filePath);
    },

    readFile(path) {
        let readStreamSync = null;
        try {
            readStreamSync = Fileio.Stream.createStreamSync(path, "r");
            let content = this.getContent(readStreamSync);
            console.info("test usb FileUtil readJsonFile finish execution" + content);
            return content;
        } catch (e) {
            console.info("test usb FileUtil readJsonFile " + e);
        } finally {
            readStreamSync.closeSync();
        }
    },

    writeTestFile(content) {
        console.info("test usb FileUtil writeJsonObj start execution");
        let filePath = dataFile + fileName;
        let writeStreamSync = null;
        try {
            writeStreamSync = Fileio.Stream.createStreamSync(filePath, "w+");
            writeStreamSync.writeSync(content);
        } catch (e) {
            console.info("test usb FileUtil writeJsonObj error: " + e);
        } finally {
            writeStreamSync.closeSync();
            console.info("test usb FileUtil writeJsonObj close sync");
        }
    },

    /**
     * Read String from disk by bundleName.
     *
     * @param {string} bundleName - bundleName as target file name
     * @return {string} - read string from file
     */
    readStringFromFile(bundleName) {
        console.info("test usb FileUtil readStringFromFile start execution");
        let filePath = baseFilePath + bundleName + ".json";
        try {
            var readStreamSync = Fileio.Stream.createStreamSync(filePath, "r");
            let content = this.getContent(readStreamSync);
            console.info("test usb FileUtil readStringFromFile finish execution" + content);
            return content;
        } catch (e) {
            console.info("test usb FileUtil readStringFromFile " + e);
        } finally {
            readStreamSync.closeSync();
        }
    },

    /**
     * Write string to a file.
     *
     * @param {string} string - target string will be written to file
     * @param {string} bundleName - bundleName as target file name
     */
    writeStringToFile(string, bundleName) {
        console.info("test usb FileUtil writeStringToFile start execution");
        let filePath = baseFilePath + bundleName + ".json";
        try {
            var writeStreamSync = Fileio.Stream.createStreamSync(filePath, "w+");
            writeStreamSync.writeSync(string);
        } catch (e) {
            console.info("test usb FileUtil writeStringToFile error: " + e);
        } finally {
            writeStreamSync.closeSync();
            console.info("test usb FileUtil writeStringToFile close sync");
        }
    },

    /**
     * Read JSON object from a file.
     *
     * @param {object} readStreamSync - stream of target file
     * @return {object} - object read from file stream
     */
    getContent(readStreamSync) {
        console.info("test usb getContent start");
        let bufArray = [];
        let totalLength = 0;
        let buf = new ArrayBuffer(READ_DATA_SIZE);
        let len = readStreamSync.readSync(buf);
        while (len != 0) {
            console.info("test usb FileUtil getContent FileIO reading " + len);
            totalLength += len;
            if (len < READ_DATA_SIZE) {
                buf = buf.slice(0, len);
                bufArray.push(buf);
                break;
            }
            bufArray.push(buf);
            buf = new ArrayBuffer(READ_DATA_SIZE);
            len = readStreamSync.readSync(buf);
        }
        console.info("test usb FileUtil getContent read finished " + totalLength);
        let contentBuf = new Uint8Array(totalLength);
        let offset = 0;
        for (let bufArr of bufArray) {
            console.info("test usb FileUtil getContent collecting " + offset);
            let uInt8Arr = new Uint8Array(bufArr);
            contentBuf.set(uInt8Arr, offset);
            offset += uInt8Arr.byteLength;
        }
        let content = String.fromCharCode.apply(null, new Uint8Array(contentBuf));
        return content;
    },

    /**
     * Remove file.
     *
     * @param {string} bundleName - bundleName as target file name
     */
    removeFile(bundleName) {
        try {
            console.info("test usb FileUtil removeFile")
            //remove file,key : bundlename
            Fileio.unlinkSync(baseFilePath + fileName)
        } catch (e) {
            console.error("test usb FileUtil removeFile delete has failed for " + e)
        }
    }
}
 